import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:mall/constants.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/services/auth.dart';
import 'package:mall/core/providers/app_model.dart';
import 'package:mall/core/services/dio_interceptor.dart';
import 'package:mall/core/services/http.dart';
import 'package:mall/core/services/new_api.dart';
import 'package:mall/generated/l10n.dart';
import 'package:mall/routes.dart';

// import 'package:mall_ad/mall_ad.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'components/no_scale_text_widget.dart';
import 'theme.dart';

/// 启动
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 先初始化 prefs
  final prefs = await SharedPreferences.getInstance();
  // 注册微信
  await fluwx.registerWxApi(appId: wx_app_id, universalLink: wx_universal_link);
  // 初始化广告
  // if (defaultTargetPlatform == TargetPlatform.android) {
  //   await MallAd.init(ad_android_app_id, ad_android_app_key);
  // } else if (defaultTargetPlatform == TargetPlatform.iOS) {
  //   await MallAd.init(ad_ios_app_id, ad_ios_app_key);
  // }
  // 再启动 App
  FlutterBugly.postCatchedException(
    () => runApp(
      MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) => AppModel(prefs)),
          Provider(create: (context) => Auth(context.read<AppModel>())),
          Provider(create: (context) => Api(context.read<AppModel>())),
          Provider(
            create: (context) {
              final appModel = context.read<AppModel>();
              final http = Http();
              http.interceptors.add(DioInterceptor(appModel));
              return NewApi(http, baseUrl: api_base_url);
            },
          ),
        ],
        child: App(),
      ),
    ),
  );
  // 配置Loading
  _configLoading();
}

/// 配置Loading
void _configLoading() {
  EasyLoading.instance
    ..userInteractions = false
    ..dismissOnTap = false;
}

/// 主APP
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 667),
      builder: () => MaterialApp(
        navigatorKey: Routes.navigatorKey,
        onGenerateRoute: Routes.generateRoute,
        builder: (context, child) {
          return Theme(
            data: buildThemeData(context),
            child: FlutterEasyLoading(
              child: NoScaleTextWidget(
                child: child,
              ),
            ),
          );
        },
        title: 'e健联',
        theme: ThemeData(
          primarySwatch: Colors.red,
          visualDensity: VisualDensity.adaptivePlatformDensity,
          fontFamily: 'SourceHanSans',
        ),
        localizationsDelegates: [
          S.delegate,
          RefreshLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate
        ],
        supportedLocales: S.delegate.supportedLocales,
        debugShowCheckedModeBanner: false,
      ),
    );
  }
}
